package com.w2cx.selenium.business;

import com.w2cx.selenium.base.DriverBase;
import com.w2cx.selenium.operate.InformationManageOperate;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import java.util.List;

public class InformationManageProcess {

    InformationManageOperate informationManageHandle;
    DriverBase driver;
   public InformationManageProcess(DriverBase driver){
       this.driver = driver;
       informationManageHandle = new InformationManageOperate(driver);
   }



    //添加资讯分类
    public void informationCategoryAdd(String categoryName){

        driver.get("http://172.16.10.68:5050/#/information/classification");
       // driver.refresh();

        //点击“资讯管理”菜单
        informationManageHandle.clickInformationManage();
        //点击“资讯分类”菜单
        informationManageHandle.clickInformationCategory();
        //点击“添加”按钮
        informationManageHandle.clickAddBtn();
        //输入要添加的分类名称
        informationManageHandle.sendCategoryName(categoryName);
        //点击保存
        informationManageHandle.clickSaveBtn();

    }

    //判断添加资讯分类是否成功
    public boolean  addCategorySuccess(String categoryName){
        //刷新列表
        driver.refresh();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        List<WebElement> list = informationManageHandle.getLevel0CategoryLists();
        System.out.println("已添加元素个数："+list.size());
        System.out.println("新添加的资讯分类名称为："+categoryName);
        for (WebElement element:list){
            //System.out.println("已添加分类："+element.getText());
            String name = element.getText();
            // System.out.println("已添加分类名称："+name);
            if(name.contains(categoryName)){
                return true;
            }
        }
        return false;
    }

    //删除资讯分类
    public void informationCategoryDel(String categoryName){
        //刷新列表
        //driver.refresh();

        //driver.get("http://172.16.10.68:5050/#/information/classification");

        //点击删除按钮
        informationManageHandle.clickDelBtn(categoryName);
    }




    //判断是否删除成功
    public boolean delSuccess(String alreadyAddCategoryName){
        //刷新列表
        driver.refresh();
        //等待3秒
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        List<WebElement> list = informationManageHandle.getLevel0CategoryLists();
        for (WebElement element:list){
            //System.out.println("删除之后已添加分类："+element.getText());
            String name = element.getText();
            if(name.contains(alreadyAddCategoryName)){
                return false;
            }
        }
        return true;
    }

    //搜索资讯分类
    public void informationCategorySearch(String categoryName){

        //输入要搜索的分类名称
        informationManageHandle.sendCategorySearchInput(categoryName);
        //点击“搜索”按钮
        informationManageHandle.clickSearchBtn();
    }

    //判断搜索是否成功
    public boolean searchCategorySuccess(){
        //获取查询分类结果
        List<WebElement> list = driver.findElements(By.className("left"));
        if(list.size() != 0){
            return true;
        }
        return false;
    }













}
